package com.xiaotu.common.util;

import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings({ "resource", "deprecation" })
public class ProxyPoolUtils
{
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ProxyPoolUtils.class);
	
	/**
	 * 测试代理是否可用
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	
	public static boolean testProxy(HttpHost proxy) throws IOException
	{
		
		HttpClient httpclient = new DefaultHttpClient();
		// HttpHost targetHost = new HttpHost("www.baidu.com");
		// HttpHost proxy = new HttpHost("111.63.135.9",80);
		httpclient.getParams()
				.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);// 连接时间
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				5000);// 数据传输时间
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
				proxy); // 这两句话去掉就是直接访问不用代理
		HttpGet httpget = new HttpGet("http://www.baidu.com");
		HttpResponse response = httpclient.execute(httpget);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
			return true;
		return false;
	}
	
}
